#include "StrHelper.h"
#include <stdio.h>
#include <stdarg.h>
#include <algorithm>



std::string str::format(const char *strFmt)
{
    return strFmt;
}

std::string str::format_pos(const char *strFmt)
{
    return strFmt;
}

const char* str::DataTraits(const std::string& ref)
{
    return ref.c_str();
}

std::string str::__format(const char *strFmt, ...)
{
#ifdef _WIN32
	va_list vl;

	va_start(vl, strFmt);
	int count = _vscprintf(strFmt, vl); //_vsctprintf(format, vl); 
	va_end(vl);

	char *pszBuf = new char[count + 1];
	va_start(vl, strFmt);
	vsprintf_s(pszBuf, count + 1, strFmt, vl);  //_vstprintf_s
	va_end(vl);
	std::string msg(pszBuf);
	delete pszBuf;
    return msg;
#else
	va_list vl;
	va_start(vl, strFmt);
	char *pszBuf = NULL;
	int count = vasprintf(&pszBuf, strFmt, vl); //_vsctprintf(format, vl); 
	va_end(vl);	

	std::string msg(pszBuf);
	free(pszBuf);
    return msg;
#endif
}


std::string& str::to_lower(std::string& strVal)
{	
	std::transform(strVal.begin(), strVal.end(), strVal.begin(), ::tolower);
	return strVal;
}

std::string str::to_lower_c(const std::string& strVal)
{
    std::string strTemp(strVal);
    std::transform(strTemp.begin(), strTemp.end(), strTemp.begin(), ::tolower);
	return strTemp;
}

std::string& str::to_upper(std::string& strVal)
{	
	std::transform(strVal.begin(), strVal.end(), strVal.begin(), ::toupper);
	return strVal;
}

std::string str::to_upper_c(const std::string& strVal)
{
    std::string strTemp(strVal);
    std::transform(strTemp.begin(), strTemp.end(), strTemp.begin(), ::toupper);
	return strTemp;
}


//去除左空格
std::string& str::ltrim(std::string& strVal, const char* pszDef)
{
	std::string::size_type sz = strVal.find_first_not_of(pszDef);
	if (sz != std::string::npos)
	{
		strVal.erase(0, sz);
	}
	else
	{
		strVal.erase(0);
	}
	return  strVal;
}
//去除右空格
std::string& str::rtrim(std::string& strVal, const char* pszDef )
{
    std::string::size_type sz = strVal.find_last_not_of(pszDef);
	if (sz != std::string::npos)
	{
		strVal.erase(sz + 1);
	}
	else
	{
		strVal.erase(0);
	}
	return  strVal;
}
//去除前后空格
std::string& str::trim(std::string& strVal, const char* pszDef )
{
	return rtrim(ltrim(strVal,pszDef), pszDef);;
}

std::string& str::replace_all(std::string& strVal, const std::string& oldStr, const std::string& newStr)
{
    std::string::size_type pos=0;
	std::string::size_type oldSize = oldStr.size();
	std::string::size_type newSize = newStr.size();
	while((pos = strVal.find(oldStr, pos))!= std::string::npos)
	{
		strVal.replace(pos, oldSize, newStr);
		pos += newSize ;
	}
    return strVal;
}


std::string str::cutstr(std::string& strVal, const std::string& strEnd, int nPos)
{
    auto nEnd = strVal.find(strEnd, nPos);
	
	if ( nEnd == std::string::npos)
	{
		return std::string();
	}
	std::string retStr = strVal.substr(nPos, nEnd - nPos);
	strVal.erase(nPos, nEnd - nPos + strEnd.length());
	return retStr;
}

std::string str::cutstr(std::string& strVal, const std::string& strStart, const std::string& strEnd, int nPos)
{
    auto nBeg = strVal.find(strStart, nPos);
	auto nEnd = strVal.find(strEnd, nPos);
	if (nBeg == std::string::npos || nEnd == std::string::npos)
	{
		return std::string();
	}
    std::string retStr = strVal.substr(nBeg + strStart.length(), nEnd - nBeg - strStart.length());
	strVal.erase(nPos, nEnd + strEnd.length());
	return retStr;
}

std::string str::cutstr_c(const std::string& strVal, const std::string& strEnd, int nPos)
{
	auto nEnd = strVal.find(strEnd, nPos);
	if ( nEnd == std::string::npos)
	{
		return std::string();
	}
	return strVal.substr(nPos, nEnd);
}

std::string str::cutstr_c(const std::string& strVal, const std::string& strStart, const std::string& strEnd, int nPos)
{
	auto nBeg = strVal.find(strStart, nPos);
	auto nEnd = strVal.find(strEnd, nPos);
	if (nBeg == std::string::npos || nEnd == std::string::npos)
	{
		return std::string();
	}
	return strVal.substr(nBeg + strStart.length(), nEnd - nBeg - strStart.length());
}